#include "system.h"
#include "delay.h"

            
__CONFIG  (HS&WDTDIS&MCLREN&PWRTEN&UNPROTECT&BORDIS&BORV21&LVPDIS);
            
unsigned char Foto;
unsigned char FotoTmp;
unsigned char Power;
unsigned char PowerEn;
unsigned char FotoEn;
unsigned char TxDel;
static bit KeyTmp;
unsigned char KeyDel;
unsigned char LevelMax;
unsigned char LevelMin;
unsigned char LevelTmp;
unsigned long koeff;
            
                    void main(void)
                    {
                    	Init();
						KeyTmp = Key;
						KeyDel=0;

							DelayMs(255);
							DelayMs(255);
                    

						if(Key==0)
							{
							DelayMs(255);
							LevelMax = Foto;				
							EEPROM_WRITE(0,LevelMax);
							while(WR)
								{}
							while(Key==0)
								{
								DelayMs(255);
								Led_Power ^= 1;
								}
							LevelMin = Foto;
							EEPROM_WRITE(1,LevelMin);
							while(WR)
								{}
							}

						LevelMax = EEPROM_READ(0);
						LevelMin = EEPROM_READ(1);
                        Led_Power = 1;


                    	for(;;)
                    	{

                    	if(PowerEn)
							PowerEn = 0;

                        if((FotoEn)&&(TxDel==0))
                            {
                                FotoEn=0;


        	                    			koeff = 0xffffffff;
            	                			koeff /= (LevelMax-LevelMin);
            	                			if(Foto>LevelMax)
            	                			    Foto = LevelMax;
											if(Foto>=LevelMin)
                    	        				koeff *= (Foto-LevelMin);
											else
												koeff = 0;
                            				koeff /= 256;
                            				koeff /= 256;
                            				koeff /= 256;  
                            				koeff /= 2;  


								if(koeff!=FotoTmp)
									{
                                	TxDel = 4;
									FotoTmp = koeff;
									}
                            }    
                                                                    
                        	if((TXIF)&&(TRMT))
                        		{
                                if(TxDel)
                                {
                                    TxDel--;
                            		switch(TxDel)
                            			{
                            			case 2:
	                            			TXREG = 0xb0|(PORTD>>4);
                            			break;
                            			case 1:
    	                        			TXREG = (PORTB&0b01111111);
                            			break;
                            			case 0:
//        	                    			koeff = 0xffffffff;
  //          	                			koeff /= (LevelMax-LevelMin);
    //        	                			if(Foto>LevelMax)
      //      	                			    Foto = LevelMax;
		//									if(Foto>=LevelMin)
          //          	        				koeff *= (Foto-LevelMin);
			//								else
			//									koeff = 0;
              //              				koeff /= 256;
                //            				koeff /= 256;
                  //          				koeff /= 256;  
                    //        				koeff /= 2;  
                      //      				TXREG = (koeff&0b01111111);                          			                            			
											TXREG = FotoTmp;
//                            				LevelTmp++;
                            			break;
                            			}
                            		}
                        		}
                    	}
                    }
    
